package com.kotasoft.ejemplohibernatevalidation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class DNIValidator implements ConstraintValidator<DNIConstraint, String> {

	private DNIConstraint constraintAnnotation;

	public void initialize(DNIConstraint constraintAnnotation) {
		this.constraintAnnotation = constraintAnnotation;
	}

	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (value == null)
			return true;

		boolean isValid = true;
		for (int i = 0; i < value.length(); i++) {
			if (i < 8)
				if (!Character.isDigit(value.charAt(i)))
					isValid = false;
			if (i == 8)
				if (!Character.isLetter(value.charAt(i)))
					isValid = false;
		}

		return isValid;
	}

}
